package com.wzx.ptplatform.controller.admin.student;

import com.wzx.ptplatform.domain.ProjectExperience;
import com.wzx.ptplatform.service.ProjectExperienceService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员学生模块——简历信息项目经历管理")
@RestController
public class AdminStudentProjectExperienceController {

    final ProjectExperienceService projectExperienceService;

    public AdminStudentProjectExperienceController(ProjectExperienceService projectExperienceService) {
        this.projectExperienceService = projectExperienceService;
    }

    @RequestMapping("admin_student_project_experience_message_list")
    public LayuiPager<ProjectExperience> adminStudentProjectExperienceMessageList(
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        ProjectExperience projectExperience = new ProjectExperience();
        Page<ProjectExperience> pager = projectExperienceService.findList(projectExperience, page - 1, limit);
        LayuiPager<ProjectExperience> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_student_project_experience_message_editView")
    public ModelAndView adminStudentProjectExperienceMessageEditView(String id, Model model) {
        model.addAttribute("projectExperience", projectExperienceService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/student/project_experience_message_edit");
    }

    @PostMapping("admin_student_project_experience_message_save_or_edit")
    public Map<String, Object> adminStudentProjectExperienceMessageSaveOrEdit(ProjectExperience projectExperience) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (projectExperienceService.saveOrEdit(projectExperience)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_student_project_experience_message_delete")
    public Map<String, Object> adminStudentProjectExperienceMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (projectExperienceService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }
}
